//
//  UserInfoViewController.swift
//  Exchange
//
//  Created by rui on 2018/5/4.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class UserInfoViewController: BaseViewController {

    private let kUserInfoTableViewCell = "UserInfoTableViewCell"

    lazy var userInfoViewModel: UserInfoViewModel = {
        let viewModel = UserInfoViewModel()
        viewModel.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_portrait {
                if isSuccess {
                    self?.tableView.reloadData()
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }
        }
        return viewModel
    }()
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: self.view.bounds, style: .plain)
        table.delegate = self
        table.dataSource = self
        table.themeBackground = ThemeManager.ThemeColorKeys.background
        table.register(UINib(nibName: kUserInfoTableViewCell, bundle: nil), forCellReuseIdentifier: kUserInfoTableViewCell)
        table.tableFooterView = UIView()
        table.themeSeparatorColor = ThemeManager.ThemeColorKeys.separator
        return table
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    func initUI() {
        self.customTitle = kText("个人信息")
        self.view.addSubview(tableView)
        let titleHeight = self.titleLabel?.frame.size.height ?? 0
        tableView.frame = CGRect(x: 0, y: titleHeight, width: self.view.frame.size.width, height: self.view.height - titleHeight)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tableView.reloadData()
    }
}

extension UserInfoViewController: UITableViewDelegate, UITableViewDataSource {
    // MARK: - UITableViewDelegate UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kUserInfoTableViewCell, for: indexPath) as! UserInfoTableViewCell
        if indexPath.row == 0 {
            cell.icon.isHidden = false
            if let portrait = AppInfo.default.portrait {
                cell.icon.sd_setImage(with: URL(string: portrait), placeholderImage: #imageLiteral(resourceName: "login_icon"))
            }else {
                cell.icon.image = #imageLiteral(resourceName: "login_icon")
            }
            cell.nickName.isHidden = true
            cell.title.text = kText("头像")
        }else {
            cell.icon.isHidden = true
            cell.nickName.isHidden = false
            cell.nickName.text = AppInfo.default.nickName
            cell.title.text = kText("昵称")
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        if indexPath.row == 1 {
            let vc = NickNameViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }else if indexPath.row == 0 {
            let manager = SelectPhotoManager.manager
            manager.controller = self
            manager.largetNum = 1
            manager.selectBlock = {(data)in
                if data.count < 1 {
                    return
                }
                if let image = data.first {
                    self.userInfoViewModel.generateIconAdress(image: image)
                }
            }
            manager.eventAddImage()
        }
    }
}
